package dhy.com.tomcat.three.response;

import dhy.com.tomcat.oneToTwo.constants.Constants;
import dhy.com.tomcat.three.request.HttpRequest;
import dhy.com.tomcat.three.response.utils.ResponseWriter;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

public class HttpResponse implements ServletResponse {
    private HttpRequest request;
    private BufferedOutputStream bufferedOutputStream;
    private ResponseWriter writer;
    private final int BUFFER_SIZE=1024;


    public HttpResponse(OutputStream output) {
        bufferedOutputStream=new BufferedOutputStream(output);
    }

    public void sendStaticResource() {
        File file = new File(Constants.WEB_ROOT, request.getUri());
        if(!file.exists())
        {
            //文件没找到
            String errorMsg="HTTP/1.1 404 File Not Found\r\n"+
                    "Content-Type:text/html;charset=UTF-8\r\n"+
                    "Content-Length: 23\r\n"+
                    "\r\n"+
                    "<h1>File Not Found</h1>";
            try {
                bufferedOutputStream.write(errorMsg.getBytes(StandardCharsets.UTF_8));
                bufferedOutputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        try (BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(file))){
            StringBuilder builder=new StringBuilder();
            String string = builder.append("HTTP/1.1 200 OK\r\n")
                    .append("Content-Type:text/html;charset=UTF-8\r\n")
                    .append("\r\n")
                    .toString();
            //写出响应头
            bufferedOutputStream.write(string.getBytes(StandardCharsets.UTF_8));
            //将文件内容读出来,放入输出流写出
            byte[] bytes = new byte[BUFFER_SIZE];
            int ch=-1;
            while((ch = bufferedInputStream.read(bytes)) != -1){
                bufferedOutputStream.write(bytes,0,ch);
            }
            bufferedOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getCharacterEncoding() {
        return null;
    }

    @Override
    public String getContentType() {
        return null;
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return null;
    }

    /**
     * <p>
     *     PrintWriter构造函数第二个参数为true,
     * </p>
     * @return
     * @throws IOException
     */
    @Override
    public PrintWriter getWriter() throws IOException {
        writer = new ResponseWriter(bufferedOutputStream, true);
        return writer;
    }

    @Override
    public void setCharacterEncoding(String s) {

    }

    @Override
    public void setContentLength(int i) {

    }

    @Override
    public void setContentLengthLong(long l) {

    }

    @Override
    public void setContentType(String s) {

    }

    @Override
    public void setBufferSize(int i) {

    }

    @Override
    public int getBufferSize() {
        return 0;
    }

    @Override
    public void flushBuffer() throws IOException {

    }

    @Override
    public void resetBuffer() {

    }

    @Override
    public boolean isCommitted() {
        return false;
    }

    @Override
    public void reset() {

    }

    @Override
    public void setLocale(Locale locale) {

    }

    @Override
    public Locale getLocale() {
        return null;
    }


    /**
     * TODO:后续处理
     */
    public void setHeader(String key, String value) {

    }

    public HttpRequest getRequest() {
        return request;
    }

    public void setRequest(HttpRequest request) {
        this.request = request;
    }
}
